import {
    Coupon
} from './coupon.model'
const coupon = new Coupon()
import Toast from '@vant/weapp/toast/toast'
Page({
    data: {
        active: 'nGet',
        // 待领取列表 status(状态):1=可领取,2=已领取,2=已过期
        nGet: '',
        // 已领取列表 status(状态):1=可用,2=已使用,3=已过期
        yGet: '',
        page: 1,
        is_click: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.getCouponData(that.data.active, that.data.page)
    },

    /**
     * 切换选项卡
     * @param {*} options
     */
    onChange(options) {
        let that = this
        that.getCouponData(options.detail.name, 1)
        that.setData({
            active: options.detail.name,
            page: 1
        })
    },

    // 获取优惠券数据
    getCouponData(active, page) {
        // 请求待领取列表 1列表类型:0=待领取,1=已领取 2page
        let type = 0
        if (active == 'yGet') {
            // 请求已领取列表
            type = 1
        }
        coupon.getCouponData(type, page).then((res) => {
            if (res.data.length < 1) {
                res.data = ''
            }
            if (type == 0) {
                this.setData({
                    nGet: res.data
                })
            } else {
                this.setData({
                    yGet: res.data
                })
            }
        }).then(() => {
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },

    /**
     * 领取优惠券
     * @param {*} options
     */
    getCoupon(options) {
        let that = this
        that.setData({
            is_click: false
        })
        // 优惠券id
        let couponId = options.currentTarget.dataset.id
        if (couponId) {
            let data = {
                id: couponId
            }
            coupon.addCouponLog(data).then((res) => {
                var type = 'success'
                var message = '领取成功'
                if (res.data.code == 902) {
                    type = 'text'
                    message = res.data.message
                }
                if (res.data.code == 904) {
                    type = 'fail'
                    message = res.data.message
                }
                Toast({
                    type: type,
                    message: message,
                    onClose: () => {
                        wx.reLaunch({
                            url: '/pages/coupon/coupon'
                        })
                    },
                })
            })
        }
    },

    /**
     * 跳转到首页
     */
    jumpIndex() {
        wx.switchTab({
            url: '../../pages/index/index'
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this
        let page = that.data.page
        page++
        let active = that.data.active
        let now_data = ''
        if (active == 'nGet') {
            now_data = that.data.nGet
        } else {
            now_data = that.data.yGet
        }
        let type = 0
        if (active == 'yGet') {
            // 请求已领取列表
            type = 1
        }
        coupon.getCouponData(type, page).then((res) => {
            let new_data = now_data.concat(res.data)
            if (active == 'nGet') {
                that.setData({
                    nGet: new_data,
                    page: page
                })
            } else {
                that.setData({
                    yGet: new_data,
                    page: page
                })
            }
        }).then(() => {
            wx.hideLoading()
        })
    }
})